﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace calculation.Models
{
    public class OrderRepository
    {
        private UchetDataContext _datacontext;

        public OrderRepository(UchetDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Order> GetOrders()
        {
            return _datacontext.Orders.OrderBy(t => t.Date);
        }

        public Order GetOrders(int orderId)
        {
            return _datacontext.Orders.SingleOrDefault(t => t.OrderId == orderId);
        }
        public Order CreateOrder(string name, Material m, Detal d) 
        {
            Order t = new Order { Name = name, StateId = 1};
            t.Date = DateTime.Now;
            t.MaterialId = m.MaterialId;
            t.DetalId = d.DetalId;
            _datacontext.Orders.InsertOnSubmit(t);
            _datacontext.SubmitChanges();
            return t;
        }
        public void UpdateOrder(Order t)
        {
            Order dbOrder = GetOrders(t.OrderId);
            dbOrder.AssignedOn = t.AssignedOn;
            dbOrder.Name = t.Name;
            
            dbOrder.StateId = t.StateId;
            dbOrder.MaterialId = t.MaterialId;
            dbOrder.DetalId = t.DetalId;
            _datacontext.SubmitChanges();
        }

        public void DeleteOrder(int orderId)
        {
            Order t = GetOrders(orderId);
            _datacontext.Orders.DeleteOnSubmit(t);
            _datacontext.SubmitChanges();   
        }

    }

}